package com.rybbaby.tss.core.utils;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.PushPayload.Builder;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JPush {
  private static final String t_sercet = "6054580d00cf95c3362a3e30";

  private static final String t_appKey = "090b9005fd564fd98bcdf59f";

  private static final String s_sercet = "de1e9e506582b295677f177c";

  private static final String s_appKey = "80395b6b73fc3fbb5efd7115";
  
  private static JPushClient  t_jpush  = new JPushClient(t_sercet, t_appKey, 1);

  private static JPushClient  s_jpush  = new JPushClient(s_sercet, s_appKey, 1);
  
  private static boolean   isnotproduce =false;

  private static boolean isproduce=true;
  
  public static void main(String[] args) {
    Map<String,String> map = new HashMap<String,String>();
    map.put("Contenttype", "task");
    map.put("type","QUESTIONUPLOAD");
    map.put("Title", "上传自拍题");
    map.put("id", "uiuiui");
    map.put("testtype", "");

     // sendpush("11","您收到一条学生上传的自拍题！","",map,true, 0,1);
         sendpush("dfsf！","",map,true, 0);
   JPushClient jpush=new JPushClient("","");
      //jpush.sendNotificationAll(alert);

   //   JPush.sendpush(openIdNew, "您收到来自亿家教的短消息！", "", map, true, isTeacher, count);

  }

  /**
   * 推送
   *
   * @param object
   *          推送的对象
   * @param isalias
   *          boolean值 true 表示推送别名 false 推送标签
   * @param isproduce
   *          true 生成环境  false 开发环境
   * @param is_teacher
   *          1 推送给老师 0 推送给学生
   */
  public static void sendpush(String object,String content,String msg_content,Map<String,String> map,boolean isalias, int is_teacher,int msgCount) {
    Builder builder = PushPayload.newBuilder();
    map.put("time", ""+new Date().getTime());
    builder
        .setPlatform(Platform.android_ios())
        .setNotification(
            Notification
                .newBuilder()
                .setAlert(content)
                .addPlatformNotification(AndroidNotification.newBuilder().setTitle("亿家教").addExtras(map).build())
                .addPlatformNotification(IosNotification.newBuilder().setBadge(msgCount).setSound("default").addExtras(map).build())
                .build())
       .setMessage( Message.newBuilder().setMsgContent(msg_content).addExtras(map).build())
        .setOptions(Options.newBuilder().setTimeToLive(86400 * 10).setApnsProduction(isproduce).build());
    if (isalias)
      builder.setAudience(Audience.alias(object));
    else
      builder.setAudience(Audience.tag(object));
    System.out.println(builder.build().toJSON());
    try {
      if (is_teacher==1)
        t_jpush.sendPush(builder.build());
       else
       s_jpush.sendPush(builder.build());
    } catch (APIConnectionException e) {
      e.printStackTrace();
    } catch (APIRequestException e) {
      e.printStackTrace();
    }

  }
  
  
  /**
   * 群发推送
   *
   * @param isalias
   *          boolean值 true 表示推送别名 false 推送标签
   * @param isproduce
   *          true 生成环境  false 开发环境
   * @param is_teacher
   *          1 推送给老师 0 推送给学生
   */
  public static void sendpush(String content,String msg_content,Map<String,String> map,boolean isalias, int is_teacher ) {
	  Builder builder = PushPayload.newBuilder();
	    map.put("time", ""+new Date().getTime());
	    builder
	        .setPlatform(Platform.android_ios())
	        .setNotification(
	            Notification
	                .newBuilder()
	                .setAlert(content)
	                
	                .addPlatformNotification(AndroidNotification.newBuilder().setTitle("亿家教").addExtras(map).build())
	                .addPlatformNotification(IosNotification.newBuilder().setBadge(+1).setSound("default").addExtras(map).build())
	                .build())
	       .setMessage( Message.newBuilder().setMsgContent(msg_content).addExtras(map).build())
	        .setOptions(Options.newBuilder().setTimeToLive(86400 * 10).setApnsProduction(isproduce).build());
	    if (isalias)
	      builder.setAudience(Audience.all());
	    else
	      builder.setAudience(Audience.all());
	    System.out.println(builder.build().toJSON());
	    try {
	      if (is_teacher==1)
	        t_jpush.sendPush(builder.build());
	       else
	       s_jpush.sendPush(builder.build());
	    } catch (APIConnectionException e) {
	      e.printStackTrace();
	    } catch (APIRequestException e) {
	      e.printStackTrace();
	    }

  }

}


